package com.ky.manage.activity.indoor;

import android.content.Intent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.ky.manage.R;
import com.ky.manage.activity.ClientInfoEditActivity;
import com.ky.manage.activity.msg.MsgListActivity;
import com.ky.manage.adapter.FragmentTabAdapter;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.fragment.IndoorSecurityCheckTabFragment;
import com.ky.manage.utils.StringUtil;
import com.ky.manage.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 户内安检
 */
public class IndoorSecurityCheckActivity extends BaseActivity {

    private List<Fragment> mFragmentList = new ArrayList<>();
    private IndoorSecurityCheckTabFragment allTabFragment, normalTabFragment,
            level1TabFragment, level2TabFragment;

    private ViewPager mPager;
    private RadioGroup mTabRg;
    private String mInputValue = "";
    private EditText searchEt;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_indoor_security_check;
    }

    @Override
    protected void initView() {
        findView(R.id.back_iv, v -> {
            finish();
        });
        findView(R.id.msg_iv, v -> {
            Intent intent = new Intent(this, MsgListActivity.class);
            intent.putExtra("type", "so_indoor_mend");
            toActivity(intent);
        });
        findView(R.id.submit_check_log, v -> {
           //新建客户信息页
            toActivity(new Intent(this, ClientInfoEditActivity.class));
        });
        mTabRg = findView(R.id.tab_rg);
        mTabRg.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.all_tab_rb) {
                mPager.setCurrentItem(0, true);
            } else if (checkedId == R.id.tab_normal_rb) {
                mPager.setCurrentItem(1, true);
            } else if (checkedId == R.id.tab_level_1_rb) {
                mPager.setCurrentItem(2, true);
            } else if (checkedId == R.id.tab_level_2_rb) {
                mPager.setCurrentItem(3, true);
            }
        });
        searchEt = findView(R.id.search_et);
        searchEt.setOnEditorActionListener((v, actionId, event) -> {
            switch (actionId) {
                case EditorInfo.IME_ACTION_SEARCH:
                    searchBykeyword(searchEt.getText().toString().trim());
                    break;
                default:
                    break;
            }
            return false;
        });
        findView(R.id.search_et_iv, v -> {
            //搜索
            searchBykeyword(searchEt.getText().toString().trim());
        });
        findView(R.id.msg_iv, v -> {
            ToastUtils.showRoundRectToast(StringUtil.getResString(R.string.to_do_prompt));
        });
        mPager = findView(R.id.view_pager);
        //全部
        allTabFragment = new IndoorSecurityCheckTabFragment("");
        //隐患（故障）等级： 1 是一级  2是二级  0  正常
        normalTabFragment = new IndoorSecurityCheckTabFragment("0");
        level1TabFragment = new IndoorSecurityCheckTabFragment("1");
        level2TabFragment = new IndoorSecurityCheckTabFragment("2");
        mFragmentList.add(allTabFragment);
        mFragmentList.add(normalTabFragment);
        mFragmentList.add(level1TabFragment);
        mFragmentList.add(level2TabFragment);

        mPager.setAdapter(new FragmentTabAdapter(getSupportFragmentManager(), mFragmentList));
        mPager.setOffscreenPageLimit(1);
        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton radioButton = (RadioButton) mTabRg.getChildAt(position);
                radioButton.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 关键词搜索
     */
    private void searchBykeyword(String searchStr) {
        Fragment tabFragment = mFragmentList.get(mPager.getCurrentItem());
        if (tabFragment instanceof IndoorSecurityCheckTabFragment) {
            ((IndoorSecurityCheckTabFragment)tabFragment).searchBykeyword(searchStr);
        }
    }

    @Override
    protected void initData() {

    }
}